package com.cyy.demo.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.math.BigDecimal;

/**
 * @Author cyy
 * @Date 2020/11/18 17:40
 * @Description
 * 转换object为指定的类型
 */
public class ConvertUtils {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static <T> T convert(Object value, Class<T> type){
        return objectMapper.convertValue(value, type);
    }

    public static void main(String[] args) {
        System.out.println(ConvertUtils.convert(1, Integer.class));
        System.out.println(ConvertUtils.convert(1, BigDecimal.class));
        System.out.println(ConvertUtils.convert("1", String.class));
        System.out.println(ConvertUtils.convert("1", Integer.class));
    }

}
